using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using System.Web.Mvc;
using FiltronASP.Helpers.HtmlTags;

namespace FiltronASP.Helpers.DropDowns
{
    /// <summary>
    /// Klasa pomocnicza do wyswietlania jezykow w drop downie.
    /// </summary>
    public static class LanguagesDropDownHelper
    {

        /// <summary>
        /// Metoda generujaca drop downa z jezykami i obrazkiem flagi.
        /// </summary>
        /// <param name="htmlHelper">html helper z c#</param>
        /// <param name="name">nazwa selecta</param>
        /// <param name="languages">jezyki</param>
        /// <param name="selectHtmlAttributes">atrybuty html selecta</param>
        /// <param name="selectedId">wybrana wersja jezykowa</param>
        /// <returns>drop down z jezykami</returns>
        public static string DropDownLanguagesList(this HtmlHelper htmlHelper, string name, 
            IEnumerable<Language> languages, IDictionary<string, string> selectHtmlAttributes, 
            int? selectedId)
        {
            if ((languages != null) && (languages.Any()))
            {
                if (selectedId == null)
                {
                    selectedId = languages.First().language_id;
                }
                UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
                TagBuilder selectTagBuilder = new TagBuilder("select");
                foreach (KeyValuePair<string, string> selectHtmlAttribute in selectHtmlAttributes)
                {
                    selectTagBuilder.MergeAttribute("name", name);
                    selectTagBuilder.MergeAttribute(selectHtmlAttribute.Key, selectHtmlAttribute.Value);
                }   //foreach
                selectTagBuilder.InnerHtml = String.Empty;
                foreach (Language language in languages)
                {
                    TagBuilder optionTagBuilder = new TagBuilder("option");
                    optionTagBuilder.MergeAttribute("class", language.language_abbr);
                    optionTagBuilder.MergeAttribute("value", language.language_id.ToString());
                    optionTagBuilder.MergeAttribute("style", "background: url(\"" + urlHelper.GenerateAdminImageLink(language.language_img) + "\") no-repeat scroll 4px 5px rgb(255, 255, 255);");
                    if (language.language_id == selectedId)
                    {
                        optionTagBuilder.MergeAttribute("selected", "selected");
                        selectTagBuilder.MergeAttribute("style", "background: url(\"" + urlHelper.GenerateAdminImageLink(language.language_img) + "\") no-repeat scroll 4px 5px rgb(255, 255, 255);");
                    }   //if
                    optionTagBuilder.InnerHtml = language.language_name;
                    selectTagBuilder.InnerHtml += optionTagBuilder.ToString(TagRenderMode.Normal);
                }   //foreach
                return selectTagBuilder.ToString(TagRenderMode.Normal);
            }   //if
            return String.Empty;
        }

    }
}
